﻿using System;
using System.Collections;
using System.Configuration;
using System.Data; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using System.Text;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.Finiance
{
    public partial class UserPerformanceView : BasePage
    {
        public string SortString
        {
            get
            {
                if (ViewState["SortString"] == null)
                {
                    ViewState["SortString"] = "CustomerName";
                }

                return ViewState["SortString"].ToString();
            }
            set { ViewState["SortString"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["UserID"] != null)
                {
                    Calculate();
                    BindGrid();
                }
            }
        }
        private void BindGrid()
        {
            StringBuilder sb = new StringBuilder();
         
            BillBll bll = new BillBll();
            int RecordCount = 0;
            DataTable dt = bll.GetCustomerPerformance(sb.ToString(), new Guid(Request.QueryString["UserID"]), SortString, AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, out RecordCount);
            gridList.DataSource = dt;
            gridList.DataBind();
            AspNetPager1.RecordCount = RecordCount;
            hfcustsum.Value = RecordCount.ToString();
        }



        protected void gridList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                LinkButton lnkbtnView = e.Item.FindControl("lnkbtnView") as LinkButton;
                DataRowView dv = e.Item.DataItem as DataRowView;
                //lnkbtnView.Attributes.Add("onclick", "openMyFullWin('CustomerBillView.aspx?CustomerCode=" + dv["cCusCode"].ToString() + "', 'viewsbilllist');");
                lnkbtnView.Attributes.Add("onclick", "openMyFullWin('/Report/ARRpt_01.aspx?CustomerCode=" + dv["cCusCode"].ToString() + "&Customername=" + dv["CustomerName"].ToString() + "', 'viewsbilllist');");
            }
        }

        protected void gridList_SortCommand(object source, Telerik.WebControls.GridSortCommandEventArgs e)
        {
            string OrderBy = e.SortExpression + " " + (e.NewSortOrder == Telerik.WebControls.GridSortOrder.Ascending ? "asc" : "desc");
            SortString = OrderBy;
            Calculate();
            BindGrid();

        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void Calculate()
        {
            
            BillBll bll = new BillBll();
            DataTable dt = bll.CalcCustomerPerformance("", new  Guid(Request.QueryString["UserID"]));

            if (dt != null && dt.Rows.Count > 0)
            {
                //hfcustsum.Value = string.Format("{0:N}", dt.Rows[0]["custs"]);
                hfNatSUm.Value = string.Format("{0:N}", dt.Rows[0]["iNatSum"]);
                hfExchSum.Value = string.Format("{0:N}", dt.Rows[0]["iExchSum"]);
                hfAccountPayable.Value = string.Format("{0:N}", dt.Rows[0]["AccountPayable"]);
                hfReward.Value = string.Format("{0:N}", dt.Rows[0]["Reward"]);
                hfFine.Value = string.Format("{0:N}", dt.Rows[0]["Fine"]);
            }
            else
            {
                //hfcustsum.Value = "0";
                hfNatSUm.Value = "0";
                hfExchSum.Value = "0";
                hfAccountPayable.Value = "0";
                hfReward.Value = "0";
                hfFine.Value = "0";
            }
        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            
            BillBll bll = new BillBll();

            DataTable dt = bll.ExportMyCustomerPerformance("",new Guid(Request.QueryString["UserID"]));

            Export.Export2Excel(dt, "客户应收账款(" + new SystemBll().Get<SysUserEntity>(new Guid(Request.QueryString["UserID"])).UserName+")", Page.Response);

        }
    }
}
